package com.dcy.daily.common.core.domain;

import com.dcy.daily.common.utils.DateUtil;
import com.dcy.daily.common.enums.ResponseCodeEnum;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

/**
 * 操作消息提醒
 * 
 */
public class ResultVO<T> implements Serializable

{
    private static final long serialVersionUID = 1L;


    /** 成功 */
    public static final String SUCCESS_CODE = ResponseCodeEnum.SUCCESS.getCode();

    /** 失败 */
    public static final String FAIL_CODE = ResponseCodeEnum.FAIL.getCode();

    /** 成功 */
    public static final String SUCCESS_MSG = ResponseCodeEnum.SUCCESS.getDesc();

    /** 失败 */
    public static final String FAIL_MSG = ResponseCodeEnum.FAIL.getDesc();

    /** 状态码 */
    private String code;

    /** 返回内容 */
    private String msg;

    /** 数据对象 */
    private T data;

    /** 时间戳 */
    private String dateTime = DateUtil.getCurrentDateTimeStr(DateUtil.FORMAT_YYYYMMDDHHMMSS);

    /**
     * 初始化一个新创建的 ResultVO 对象，使其表示一个空消息。
     */
    public ResultVO()
    {
    }

    private static <T> ResultVO<T> restResult(String code, String msg, T data)
    {
        ResultVO<T> vo = new ResultVO<>();
        vo.setCode(code);
        vo.setMsg(msg);
        vo.setData(data);
        return vo;
    }

    /**
     * 返回成功消息
     * 
     * @return 成功消息
     */
    public static <T> ResultVO<T> success()
    {
        return restResult(SUCCESS_CODE,SUCCESS_MSG, null);
    }

    /**
     * 返回成功数据
     * 
     * @return 成功消息
     */
    public static <T> ResultVO<T> success(T data)
    {
        return restResult(SUCCESS_CODE,SUCCESS_MSG, data);
    }

    /**
     * 返回成功消息
     * 
     * @param msg 返回内容
     * @return 成功消息
     */
    public static <T> ResultVO<T> success(String msg)
    {
        return restResult(SUCCESS_CODE,msg, null);
    }

    public static <T> ResultVO<T> success(String code, String msg)
    {
        return restResult(code,msg, null);
    }

    /**
     * 返回成功消息
     * 
     * @param msg 返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static <T> ResultVO<T> success(String msg, T data)
    {
        return restResult(SUCCESS_CODE, msg, data);
    }

    public static <T> ResultVO<T> success(String code, String msg, T data)
    {
        return restResult(code, msg, data);
    }

    /**
     * 返回错误消息
     * 
     */
    public static <T> ResultVO<T> error()
    {
        return restResult(FAIL_CODE,FAIL_MSG,null);
    }

    /**
     * 返回错误消息
     * 
     * @param msg 返回内容
     * @return 警告消息
     */
    public static <T> ResultVO<T> error(String msg)
    {
        return restResult(FAIL_CODE,msg,null);
    }

    public static <T> ResultVO<T> error(String code, String msg)
    {
        return restResult(code,msg,null);
    }

    /**
     * 返回错误消息
     * 
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static <T> ResultVO<T> error(String msg, T data)
    {
        return restResult(FAIL_CODE,msg,data);
    }

    public static <T> ResultVO<T> error(String code, String msg, T data)
    {
        return restResult(code,msg,data);
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }

    public String getDateTime() {
        return dateTime;
    }

    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
            .append("code", getCode())
            .append("msg", getMsg())
            .append("data", getData())
            .append("dateTime",getDateTime())
            .toString();
    }
}
